package edu.ruc.cluster.action;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import edu.ruc.cluster.util.AbstractInteractiveCommand;

/**
 * 2012-12-05
 * @author deke
 * Establish ssh login without password from A machine(information obtained from constructor)
 * to B machines(information input through dealssh(String host, String passwd) function
 */
public class NoPasswdSSHCmd extends AbstractInteractiveCommand {

	public NoPasswdSSHCmd(String host, String passwd){
		super(host, passwd);
	}
	@Override
	public void interactProcess(String cmd) {
		// TODO Auto-generated method stub
		String[] commands = cmd.split("xxx");
		try{
			int count = 1;
			
			for(int i = 0 ; i < commands.length ; i ++){
				writor.write((commands[i]+ "\n").getBytes());
				writor.flush();
							
		    	char[] cbuf = new char[1024];			
				int ret = reader.read(cbuf);
				
				while(ret != -1){
					String info = new String(cbuf, 0, ret);
					System.out.print(info);
					if(info.contains("(yes/no)") || info.contains("(y/n)")){
						writor.write(("yes" + "\n").getBytes());
				    	writor.flush();
					}else if(info.contains(property.getProperty("hostssh") + "'s password")){
						writor.write((property.getProperty("passwdssh") + "\n").getBytes());
						writor.flush();
					}
					
					if(info.contains("[" + getUser()+ "@" )){						
						if(count == 0){
							break;
						}
						count --;
					}
					ret = reader.read(cbuf);
				}
			}
		}catch(IOException e){
			logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
		}
	}
	public void masterssh(String hostt, String passwd){
		
		Map<String, String> info = new HashMap<String, String>();
		info.put("hostssh", hostt);
		info.put("passwdssh", passwd);		
		setProperty(info);
		String cmd1 = "ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa";
		String cmd2 = "cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys";
//		String cmd3 = "ssh-copy-id -i ~/.ssh/id_rsa.pub " + hostt;		
//		String cmd4 = "chmod 700 ~/.ssh/";
//		String cmd5 = "chmod 600 ~/.ssh/authorized_keys";
		dealcmd(cmd1 + "xxx" + cmd2);

	}
	/**
	 * About slaves
	 * @param hostt : usage of this class is to establish 'ssh hosttt' without password
	 * @param passwd  
	 */
	public void ssh(String hostt, String passwd){
		
		Map<String, String> info = new HashMap<String, String>();
		info.put("hostssh", hostt);
		info.put("passwdssh", passwd);		
		setProperty(info);
		
			// In practice, there's more complicated situations like authentication of ~/.ssh
			// which needs do the following in hostt(slave) machine:
			//		* chmod 700 ~/.ssh/
			//		* chmod 600 ~/.ssh/authorized_keys
			// However, this simple edition just omit that

//		String cmd1 = "ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa";
//		String cmd2 = "cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys";
		String cmd3 = "ssh-copy-id -i ~/.ssh/id_rsa.pub " + hostt;
		
		String cmd4 = "chmod 700 ~/.ssh/";
		String cmd5 = "chmod 600 ~/.ssh/authorized_keys";
		dealcmd(cmd3 + "xxx" + cmd4 + "xxx" + cmd5);
	}
	
	public static void main(String[] args){
		
			// setup ssh login without password from 'host' to 'hostssh'
		String host = "abc@dlnode17";
		String passwd = "123456";
		NoPasswdSSHCmd npssh = new NoPasswdSSHCmd(host, passwd);
		
		String hostssh = "abc@dlnode27";
		String passwdssh = "123456";
		npssh.ssh(hostssh, passwdssh);
	}

}
